import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { environment } from 'src/environments/environment';
@Injectable({
  providedIn: 'root'
})
export class UserService {
  
  data='/User'
  constructor(private http: HttpClient) { }
  private createPaginationParams(currentPage: number, itemsPerPage: number): HttpParams {
    return new HttpParams()
      .set('page', currentPage.toString())
      .set('pageSize', itemsPerPage.toString());
  }// 创建分页参数
  GetAllDepartments(currentPage: number, itemsPerPage: number){
    const GetAllDepartments = '/GetPagedDepartmentsWithChildren';
    const params = this.createPaginationParams(currentPage, itemsPerPage);
    return this.http.get(environment.apiUrl+ this.data+GetAllDepartments,{params});
  }// 获取所有部门
  CreateDepartment(data:any){
    const CreateDepartment = '/CreateDepartment';
    return this.http.post(environment.apiUrl+ this.data+CreateDepartment,data);
  }// 创建部门
  DeleteDepartment(id:any){
    const DeleteDepartment = '/DeleteDepartment';
    return this.http.delete(environment.apiUrl+ this.data+DeleteDepartment,{params:{id}});
  }// 删除部门
  UpdateDepartment(id:any,data:any){
    const UpdateDepartment = '/UpdateDepartment';
    return this.http.put(environment.apiUrl+ this.data+UpdateDepartment,data,{params:{id}});
  }// 修改部门
  CreateEmploy(data:any){
    const CreateEmploy = '/CreateUser';
    return this.http.post(environment.apiUrl+ this.data+CreateEmploy,data);
  }// 创建员工
  GetAllEmploy(currentPage: number, itemsPerPage: number){
    const GetAllEmploy = '/GetUsers';
    return this.http.get(environment.apiUrl+ this.data+GetAllEmploy,{params:{
      PageNumber:currentPage,
      PageSize:itemsPerPage
    }});
  }// 获取所有员工
  DisableUsers(id:any){
    const DisableUsers = '/DisableUsers';
    return this.http.put(environment.apiUrl+ this.data+DisableUsers,id);
  }// 禁用员工
  EnableUsers(id:any){
    const EnableUsers = '/EnableUsers';
    return this.http.put(environment.apiUrl+ this.data+EnableUsers,id);
  }// 启用员工
  DeleteUsers(id:any){
    const DeleteUsers = '/DeleteUsers';
    const options = {
      body: id,
      responseType: 'text' as 'json'
    };
    return this.http.delete(environment.apiUrl+ this.data+DeleteUsers,options);
  }// 删除员工
  ChangePassword(data:any,employeeId:any){
    const ChangePassword = '/ChangePassword';
    return this.http.put(environment.apiUrl+ this.data+ChangePassword,data,{params:{employeeId}, responseType: 'text'});
  }// 修改密码
  UpdateUser(data:any,employeeId:any){
    const UpdateUser = '/UpdateUser';
    return this.http.put(environment.apiUrl+ this.data+UpdateUser,data,{params:{employeeId}, responseType: 'text'});
  }// 修改个人信息
  GetUserByEmployeeCode(employeeCode:any){
    const GetUserByEmployeeCode = '/GetUserByEmployeeCode';
    return this.http.get(environment.apiUrl+ this.data+GetUserByEmployeeCode,{params:{employeeCode}});
  }// 获取个人信息
  GetUserByDepartmentId(DepartmentId?: number, Status?: string, SearchTerm?: string, PageNumber?: number, PageSize?: number) {
    const GetUserByDepartmentId = '/GetUsers';
    let params = {};
    if (DepartmentId) {
      params['DepartmentId'] = DepartmentId;
    }
    if (Status) {
      params['Status'] = Status;
    }
    if (SearchTerm) {
      params['SearchTerm'] = SearchTerm;
    }
    params['PageNumber'] = PageNumber;
    params['PageSize'] = PageSize;

    return this.http.get(environment.apiUrl+ this.data+GetUserByDepartmentId, { params });
}

  DownloadUserFileModel(){
    const DownloadUserFileModel = '/DownloadUserFileModel';
    return this.http.get(environment.apiUrl+'/Excel'+DownloadUserFileModel,{responseType: 'blob'});
  }// 下载模板
  importUsersFromExcel(file: File) {
    const formData = new FormData();
    formData.append('file', file);

    const url = `${environment.apiUrl}/Excel/ImportUsersFromExcel`;
    return this.http.post(url, formData, {responseType: 'text'});
  }// 导入员工
  DownloadUserFile(){
    const DownloadUserFile = '/DownloadUserFile';
    return this.http.get(environment.apiUrl+'/Excel'+DownloadUserFile,{responseType: 'blob'});
  }// 下载导入结果
  Login(data:any){
    const Login = '/Login';
    return this.http.post(environment.apiUrl+ this.data+Login,data);
  }// 登录
  DownloadDepartmentFileModel(){
    const DownloadDepartmentFileModel = '/DownloadDepartmentFileModel';
    return this.http.get(environment.apiUrl+'/Excel'+DownloadDepartmentFileModel,{responseType: 'blob'});
  }// 下载部门模板
 ImportDepartmentsFromExcel(file: File){
  const formData = new FormData();
  formData.append('file', file);
    const url = `${environment.apiUrl}/Excel/ImportDepartmentsFromExcel`;
    return this.http.post(url, formData, {responseType: 'text'});
  }// 导入部门

  GetUserById(id:any){
    const GetUserById = '/GetUserById';
    return this.http.get(environment.apiUrl+ this.data+GetUserById,{params:{id}});
  }// 获取用户


  AdminResetUserPassword(employeeId: any, newPassword: any) {
    const AdminResetUserPassword = '/AdminResetUserPassword';
    return this.http.put(environment.apiUrl + this.data + AdminResetUserPassword, null, {
      params: { employeeId, newPassword },
      responseType: 'text'
    });
  } // 重置密码
  GetDepartmentById(id:any){
    const GetDepartmentById = '/GetDepartmentById';
    return this.http.get(environment.apiUrl+ this.data+GetDepartmentById,{params:{id}});
  }// 获取部门
}

